//
//  HomeViewController.swift
//  Weibo(Swift)
//
//  Created by 阳 on 16/7/9.
//  Copyright © 2016年 阳. All rights reserved.
//

import UIKit
private let identifier : String = "homeCell"
private let beginNum : Int = 1000
class HomeViewController: RootViewController {

    lazy var dataArray : [Int] = []
    
    var isPullUp : Bool = false
    lazy var statusArray : [StatusModel] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: identifier)
        self.loadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        
    }
    
    

}
extension HomeViewController
{
    override func loadData()
    {
        if !isPullUp //下拉
        {
            self.refreshCotroller.beginRefreshing()
            let since_id : Int64 = statusArray.first?.id ?? 0
            print(since_id)
            let max_id :Int64 = 0
            NetworkManager.sharedManager.requestStatus(since_id, max_id: max_id, callBack: { (response) -> Void in
                print(response)
                var responseArray : [StatusModel] = []
                if let statusResponse = response?["statuses"] as? [[String : AnyObject]]
                {
                    for dic in statusResponse
                    {
                        let model = StatusModel(dic: dic)
                        responseArray.append(model)
                    }
                    self.statusArray =  self.statusArray + responseArray
                    
                }
                
                self.refreshCotroller.endRefreshing()
                self.tableView.reloadData()
            })

        }
        else
        {
            self.indicator.startAnimating()
            let max_id : Int64 = statusArray.last?.id ?? 0
            print(max_id)
            let since_id : Int64 = 0
            NetworkManager.sharedManager.requestStatus(since_id, max_id: max_id, callBack: { (response) -> Void in
                print(response)
                var statusArr : [StatusModel] = []
              if let responseArr = response!["statuses"] as? [[String : AnyObject]]
              {
                for dic in responseArr
                {
                     let model = StatusModel(dic: dic)
                    statusArr.append(model)
                }
                self.statusArray = self.statusArray + statusArr
            
              }
                
                self.indicator.stopAnimating()
                self.tableView.reloadData()
                self.isPullUp = false
            })
           
        }
    }
}
//MARK: - TableViewDataSource & Delegate

extension HomeViewController
{
   override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == self.dataArray.count - 1
        {
            isPullUp = true
            self.loadData()
        }
    }
}
extension HomeViewController
{
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statusArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
        let model = statusArray[indexPath.row]
        cell.textLabel?.text = model.text
        
        return cell
    }
}
